package org.item.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.item.common.bean.SysRole;
import org.item.common.service.IRoleService;
import org.item.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @PostMapping("/")
    public ResultData insert(@RequestBody SysRole role) {
        SysRole data = roleService.insert(role);
        return ResultData.success(data);
    }

    @PutMapping("/")
    public ResultData update(@RequestBody SysRole role) {
        SysRole data = roleService.update(role);
        return ResultData.success(data);
    }

    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = roleService.delete(id);
        if (result >= 1) {
            return ResultData.success(200, "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        SysRole data = roleService.getById(id);
        return ResultData.success(data);
    }

    @GetMapping("/list")
    public ResultData getList(SysRole role) {
        return ResultData.success(roleService.getList(role));
    }

    @GetMapping("/page")
    public ResultData getListByPage(Page page, SysRole role) {
        IPage data = roleService.getListPage(page, role);
        return ResultData.success(data);
    }

    @PutMapping("/status/{id}")
    public ResultData setStatus(@PathVariable Integer id) {
        int result = roleService.setStatus(id);
        if (result >= 1) {
            return ResultData.success(200, "状态更新成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @PutMapping("/permission")
    public ResultData assignPermission(@RequestBody SysRole role) {
        SysRole data = roleService.assignPermission(role);
        return ResultData.success(data);
    }
}
